define(["angularAMD", "moment", "module/system/program/myFavourite/favouriteService"], function (moment) {
    return ['$rootScope', "$scope", "$filter", "$stateParams", "config", "MMWService", "RouterExtendProvider", "$mdDialog", "favouriteService",
    function ($rootScope, $scope, $filter, $stateParams, config, $MMWService, $RouterExtendProvider, $mdDialog, $favouriteService) {
        // 20220726 modify by Alan for#107965 : 配合規格進行調整
        let isFromHome = false;

        $scope.favouriteInfo = {
            UserNo: '',
            FavouriteList: [],
            DefaultProgram : ''
        };
        function userCustomReportGet(callback){
			$MMWService.sendToServer({
				uri : 'ServicesSTD.Module_SYS.user_custom_report_info_get',
				content : {},
				success : function(data){
					customMenuChilld = data;
                   
                    customMenuChilld.report_info.forEach(item=>{
                        $scope.favouriteInfo.FavouriteList.forEach(function(flag){
                                item['isShow'] =  true ;
                                item['displayText']= item.report_description;
                                item['name']= item.report_id;
                                item['icon']= "W_Com_User_Edit_nor_32";
                                item['title']= item.report_description;
                        });
                    });
                    callback(data);
                
				}
			});
		}
        $scope.init = function(){
            $favouriteService.get(config.cache.account, (data) => {
                $scope.favouriteInfo = data;

                // 我的最爱编辑重置菜单拼接  20231013 modify by shiwl for#0144676: 客户我的最爱自定义报表无法显示内容，麻烦看一下
                userCustomReportGet(function(data){       
                  $rootScope.menu.forEach(function(item){
                    if(item.name=="modelC"){
                        item['childs'] =  data.report_info;
                    }
    
                  });
               });

            });
        }

        $scope.back = function(){
            //返回首页，传入true表示使用退出
            $scope.$root.hideMenu();
            $scope.$root.changeProgram('home', true);//动画
        }

        $scope.confirm = function(){
            //确认按钮
            $scope.$root.hideMenu();
            $scope.$root.changeProgram('home', true);
        }

        //sMES 2.1.0 以后新增
        $scope.final = function(callback){
            //当离开此程序的时候一定会执行此函式
            callback();//结束程序
        }

        $scope.toggleFavourite = function(program) {
            const programIndex = $scope.favouriteInfo.FavouriteList.indexOf(program.name)
            if(programIndex >= 0) {
                $scope.favouriteInfo.FavouriteList.splice(programIndex, 1);
            } else if($scope.favouriteInfo.FavouriteList.length === 1 && $scope.favouriteInfo.FavouriteList[0] === ''){
                $scope.favouriteInfo.FavouriteList[0] = program.name;
            } else {
                $scope.favouriteInfo.FavouriteList.push(program.name);
            }
            console.log($scope.favouriteInfo.FavouriteList);
        }

        $scope.isTempFavourite = function(program) {
            return $scope.favouriteInfo.FavouriteList.includes(program.name) > 0 && program.isShow
        }

        // 20230310 modify by Alan for#129828 : 我的最愛報錯
        $scope.isModuleHaveProgram = function(module) {
            return module.childs && module.childs.length > 0 
                && module.childs.some(program => {
                    return  program.isShow
                });
        }

        $scope.saveFavourite = function() {
            const option = $scope.favouriteInfo;
            option.type = 1;
            $favouriteService.setFavouriteList(option, () => $RouterExtendProvider.myFavourite('main'));
        }

        $scope.isTempFavouriteEmpty = function() {
            return $scope.favouriteInfo.FavouriteList.length === 0;
        }
    }
]});
